<?php
namespace Common\Model;
use Common\Model\BaseModel;
class StaffModel extends BaseModel
{
    protected $tableName = 'staff';

    protected $_validate = array(       //查验提交数据时是否有空数据。
        array('staff_id','require','教职工编号不能为空!'),
        array('staff_id','','教职工编号已经存在！',0,'unique',1),
        array('password','require','密码不能为空！'),
    );
    
    public function getStaffById($staff_id,$fields="*"){
        $staff_id = intval($staff_id);
        $where = " staff_id={$staff_id} ";
        $res = $this->field($fields)->where($where)->select();
        return !empty($res) ? $res[0] : array();
    }

    public function checkLogin($user_name,$pass){

        $row = $this->field('staff_id,staff_name,password,is_admin_user,staff.role_id,staff.rooms,role.role_name,role.`purview_id`')
            ->join('role on staff.role_id= role.role_id','LEFT')
            ->where("staff_id='{$user_name}'")
            ->select();

        if(!empty($row) && pwCheck($pass,$row[0]['password'])){
            $user['staff_id'] = $row[0]['staff_id'];
            $user['role'] = $row[0]['role_name'];
            $user['role_id'] = $row[0]['role_id'];
            $user['nickname']= $row[0]['staff_name'];
            $user['is_admin_user']= $row[0]['is_admin_user'];
//            dump($row[0]['rooms'])
            $user['rooms']= explode('|',$row[0]['rooms']);
            $map['id']    = array('in',$row[0]['purview_id']);
            return $user;
        }
        return false;
    }
    public function getStaffList($where){
        $list = $this->where($where)->select();
        return $list;
    }

    public function addStaff($post)
    {

        $ret = array('status' => '', 'info' => '');
        $data = $this->returnData($post);
        if ($this->create($data)) {
            $res = null == $data['staff_id'] ? $this->add($data) : $this->save($data);
            if (false !== $res) {
                $ret['status'] = true;
                $ret['info'] = "Success";
            } else {
                $ret['status'] = false;
                $ret['info'] = $this->getDbError();
            }
        } else {
            $ret['status'] = false;
            $ret['info'] = $this->getError();
        }

        return $ret;
    }
    public function editStaff($post)
    {

        $ret = array('status' => '', 'info' => '');
        $data = $this->returnData($post);

        if ($this->create($data)) {
            $res = $this->save($data);
            if (false !== $res) {
                $ret['status'] = true;
                $ret['info'] = "Success";
            } else {
                $ret['status'] = false;
                $ret['info'] = $this->getDbError();
            }
        } else {
            $ret['status'] = false;
            $ret['info'] = $this->getError();
        }

        return $ret;
    }
    public function returnData($post)
    {
        $return['staff_id'] = !empty($post['staff_id']) ? intval($post['staff_id']) : null;
        $return['password'] = !empty($post['password']) ? $this->verifyPass($return['staff_id'],$post['password']) : '';
        $return['staff_name'] = !empty($post['staff_name']) ? addslashes($post['staff_name']) : '';
        $return['staff_unit'] = !empty($post['staff_unit']) ? addslashes($post['staff_unit']) : '';
        $return['staff_tel'] = !empty($post['staff_tel']) ? addslashes($post['staff_tel']) : '';
        $return['staff_email'] = !empty($post['staff_email']) ? addslashes($post['staff_email']) : '';
        $return['staff_ins'] = !empty($post['staff_ins']) ? addslashes($post['staff_ins']) : '';
        $return['role_id'] = !empty($post['role_id']) ? intval($post['role_id']) : 0;
        $return['is_admin_user'] = !empty($post['is_admin_user']) ? intval($post['is_admin_user']) : 1 ;
        $return['tmgrs'] = !empty($post['tmgrs']) ? intval($post['tmgrs']) : 0 ;
        $return['mulit_role'] = !empty($post['mulit_role']) ? intval($post['mulit_role']) : 1 ;
        return $return;
    }
    public function verifyPass($staff_id,$pass){
        $staff_info = $this->getStaffById($staff_id);
        if(!empty($staff_info) && $pass==$staff_info['password']){
            return $pass;
        }else{
            return md5($pass);
        }
    }
    public function getManagerList(){
        $where = " role_id=".self::ROLE_TEACHER_MGR;
        $res = $this->where($where)->select();
        return !empty($res) ? $res : array();
    }

    public function getStaffByTmgrId($staff_id,$fields="*"){
        $staff_id = intval($staff_id);
        $where = " tmgrs={$staff_id} ";
        $res = $this->field($fields)->where($where)->select();
        return !empty($res) ? $res: array();
    }
}